package vip.idiy.anwei_android_iot;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.content.res.AppCompatResources;

import android.content.Intent;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.commons.lang3.StringUtils;

import vip.idiy.anwei_android_iot.dialog.CustomDialog;
import vip.idiy.anwei_android_iot.dialog.IconSelectDialog;
import vip.idiy.anwei_android_iot.domin.DeviceScene;
import vip.idiy.anwei_android_iot.domin.IconEntity;
import vip.idiy.anwei_android_iot.enums.SceneIconEnum;
import vip.idiy.anwei_android_iot.utils.ActivityFactory;
import vip.idiy.anwei_android_iot.utils.DatabaseUtil;
import vip.idiy.anwei_android_iot.utils.IotUtil;
import vip.idiy.anwei_android_iot.utils.ViewUtil;
import vip.idiy.anwei_android_iot.utils.dbHandle.SceneDataHandle;
import vip.idiy.anwei_android_iot.views.LayoutBasicInfo;
import vip.idiy.anwei_android_iot.views.MyImageButton;

/**
 * 编辑 添加位置
 */
public class AddEditSceneActivity extends AppCompatActivity {

    private final String TAG = "myTag " + " AddEditSceneActivity ";

    private LinearLayout add_edit_scene_container;
    private SceneDataHandle sceneDataHandle;
    private LayoutBasicInfo layout_basic_info_scene_name;
    private LayoutBasicInfo layout_basic_info_scene_icon;
    private DeviceScene deviceScene;
    private boolean result = false;
    private int mode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_edit_scene);
        // 设置设透明状态栏,导航栏. 状态栏字体修改为黑色
        ViewUtil.setViewStyle(this, true);

        DatabaseUtil databaseUtil = DatabaseUtil.getInstance(this);
        sceneDataHandle = new SceneDataHandle(databaseUtil);

        deviceScene = new DeviceScene();
        //初始化窗口
        initView();
    }

    /**
     * 初始化窗口
     */
    private void initView() {
        // 头部标题
        TextView layout_reuse_head_tv_title = findViewById(R.id.layout_reuse_head_tv_title);
        // 返回按钮
        findViewById(R.id.layout_reuse_head_return_myImageButton).setOnClickListener(v -> finish());

        // 判断模式
        Intent intent = getIntent();
        mode = intent.getIntExtra("mode", 0);
        String par = intent.getStringExtra("par");

        // 添加或编辑按钮
        MyImageButton add_edit_scene_button = findViewById(R.id.add_edit_scene_button);
        add_edit_scene_button.setOnClickListener(v -> addAndEditSceneData(mode));

        // 取消按钮
        findViewById(R.id.cancel_scene_button).setOnClickListener(v -> finish());

        // 根据模式设置按钮样式
        switch (mode) {
            case GlobalApplication.EDIT_MODE: // 编辑
                layout_reuse_head_tv_title.setText(R.string.title_edit_scene);
                add_edit_scene_button.setImageResource(R.mipmap.icon_edit_white);
                add_edit_scene_button.setText(getString(R.string.data_edit));
                add_edit_scene_button.setBackground(AppCompatResources.getDrawable(AddEditSceneActivity.this, R.drawable.ripple_blue));
                break;

            case GlobalApplication.ADD_MODE: // 增加
                layout_reuse_head_tv_title.setText(R.string.title_add_scene);
                add_edit_scene_button.setImageResource(R.mipmap.icon_add_white);
                add_edit_scene_button.setText(getString(R.string.data_add));

                break;
            case GlobalApplication.REMOVE_MODE:
                layout_reuse_head_tv_title.setText(R.string.title_remove_scene);
                add_edit_scene_button.setImageResource(R.mipmap.icon_remove_white);
                add_edit_scene_button.setText(getString(R.string.data_remove));
                add_edit_scene_button.setBackground(AppCompatResources.getDrawable(AddEditSceneActivity.this, R.drawable.ripple_red));
                break;
        }
        // LinearLayout  View容器
        add_edit_scene_container = findViewById(R.id.add_edit_scene_container);
        // 位置名称
        layout_basic_info_scene_name = new LayoutBasicInfo(this)
                .init(true, false, false, true, false, false)
                .setLeftText(getString(R.string.table_scene_name))
                .setLineBackground(R.drawable.ripple_gray)
                .setLeftImg(R.mipmap.icon_place);
        // icon
        layout_basic_info_scene_icon = new LayoutBasicInfo(this)
                .init(true, false, true, false, false, true)
                .setLeftImg(R.mipmap.icon_pic)
                .setLeftText(getString(R.string.table_scene_icon))
                .setRightImg(R.mipmap.icon_right)
                .setLineBackground(R.drawable.ripple_gray)
                .setLineClick(v -> {
                    IconSelectDialog dialog = IconSelectDialog.newInstance(
                            AddEditSceneActivity.this,
                            getString(R.string.please_select_scene_icon),
                            entity -> {
                                layout_basic_info_scene_icon.setCentreImg(entity.getIcon());
                                layout_basic_info_scene_icon.setCentreIconName(entity.getIconName());
                            }, SceneIconEnum.toList());
                    dialog.show();
                });
        // 如果是编辑和删除模式, 先获取记录
        if (mode == GlobalApplication.EDIT_MODE || mode == GlobalApplication.REMOVE_MODE) {
            deviceScene = sceneDataHandle.selectBySceneName(par);
            // 设置编辑框显示文本
            layout_basic_info_scene_name.setCentreEditContent(deviceScene.getSceneName());
            // 设置记录的位置的图标
            layout_basic_info_scene_icon.setCentreImg(IotUtil.checkIcon(deviceScene.getSceneIcon()).getIcon());
            layout_basic_info_scene_icon.setCentreIconName(deviceScene.getSceneIcon());
        }
        add_edit_scene_container.addView(layout_basic_info_scene_name);
        add_edit_scene_container.addView(layout_basic_info_scene_icon);


    }

    /**
     * 添加或编辑
     *
     * @param mode
     */
    private void addAndEditSceneData(int mode) {
        String sceneName = layout_basic_info_scene_name.getCentreEditContent();
        String iconName = layout_basic_info_scene_icon.getCentreIconName();
        // 判断设备位置是否为空
        if (StringUtils.isEmpty(sceneName)) {
            Toast.makeText(this, R.string.please_input_scene_name, Toast.LENGTH_SHORT).show();
            return;
        }
        // 判断位置图片是否为空
        if (StringUtils.isEmpty(iconName)) {
            Toast.makeText(this, R.string.please_select_scene_icon, Toast.LENGTH_SHORT).show();
            return;
        }
        // 判断填写的设备位置是否已存在
        if (mode == GlobalApplication.ADD_MODE) {
            if (sceneDataHandle.isExist(sceneName)) {
                Toast.makeText(this, R.string.data_is_exist, Toast.LENGTH_SHORT).show();
                return;
            }
        }
        deviceScene.setSceneName(sceneName);
        deviceScene.setSceneIcon(iconName);
        switch (mode) {
            case GlobalApplication.EDIT_MODE:
                result = sceneDataHandle.update(deviceScene);
                Toast.makeText(this, result ? R.string.edit_success : R.string.edit_fail, Toast.LENGTH_SHORT).show();
                break;
            case GlobalApplication.ADD_MODE:
                result = sceneDataHandle.insert(deviceScene);
                Toast.makeText(this, result ? R.string.add_success : R.string.add_fail, Toast.LENGTH_SHORT).show();
                break;
            case GlobalApplication.REMOVE_MODE:
                // 删除必须确认再删除
                CustomDialog customDialog = new CustomDialog(this, new CustomDialog.CustomDialogClickListener() {
                    @Override
                    public void doConfirm() {
                        result = sceneDataHandle.delete(deviceScene);
                        Toast.makeText(AddEditSceneActivity.this, result ? R.string.remove_success : R.string.remove_fail, Toast.LENGTH_SHORT).show();
                        finish();
                    }

                    @Override
                    public void doCancel() {

                    }
                });
                customDialog.setTitle(getString(R.string.dialog_title_scene_remove));
                customDialog.setMessage(getString(R.string.dialog_msg_is_remove_data, deviceScene.getSceneName()));
                customDialog.show();
                break;
        }
        // 如果修改 添加 ,直接关闭界面
        if (result)
            finish();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 关闭后刷新数据
        ActivityFactory.sceneHandleActivity_flushList(mode);
    }
}